package cn.gwm.flink.streaming.dws.reservecharge.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.ArrayList;

/**
 * @author Liu.Nanfang
 * @date 2022/7/15 13:46
 * @description 模拟生产数据
 */
public class CustomJsonSource implements SourceFunction<String> {

    public static String txtJson = "{\"version\":1,\"oBC_ChrgrOutputVolt\":334.5,\"end_BMS_SOH_COR\":-999999,\"end_chrgConnectTid\":1678733483000,\"chargeType\":\"3\",\"chrgConnectTid\":1678720495000,\"deviceId\":\"TLGX0314173051\",\"datedt\":\"2023-03-13\",\"end_BMS_BattVolt_RM\":342,\"eVCC_EVSEOutputC\":-999999,\"end_OBC_ChrgrOutputVolt\":337.28,\"aCAmbTemp\":5.5,\"adCode\":\"130635\",\"item_time\":1678720675000,\"eVCC_EVSEOutputP\":-999999,\"longitude\":115.59721352628934,\"expensivePower\":10,\"end_BMS_SOH\":100,\"eVCC_EVSEOutputV\":-999999,\"bMS_SOH_COR\":-999999,\"end_BMS_ModuleTempaverage\":8,\"end_ACAmbTemp\":5.5,\"chargeModel\":3,\"faultCode\":\"-999999\",\"model_code\":\"CC7000TESTABEV\",\"effectiveChargeTime\":12808,\"bMS_BattVolt_RM\":335,\"end_BMS_SOE\":41.4,\"end_BMS_SOC\":90.0,\"bMS_SWVersion\":\"4961\",\"vehicletype\":\"ES13\",\"aoiType\":\"120302\",\"end_BMS_BattCurr_RM\":-8,\"bMS_BattCurr_RM\":-8,\"latitude\":38.50839316209733,\"endFlag\":\"-999999\",\"bMS_ModuleTempaverage\":8,\"chargeStatus\":\"6\",\"end_EVCC_EVSEOutputC\":-999999,\"chargeSuccOrFail\":1,\"bMS_RMC_ModuleTempMin\":7,\"endChargeTime\":1678733483000,\"startChargeTime\":1678720675000,\"tbox_BattSOCLim\":-999999,\"bMS_ChrgTime\":-999999,\"end_BMS_RMC_ModuleTempMax\":8,\"bMS_DCChrgConnect\":0,\"discountTime\":5,\"bMS_SOH\":100,\"bMS_RMC_ModuleTempMax\":8,\"end_EVCC_EVSEOutputV\":-999999,\"end_OBC_ChrgrOutputCurr\":8.0,\"bMS_SOC\":45.0,\"expensiveTime\":1,\"bMS_SOE\":20.5,\"end_EVCC_EVSEOutputP\":-999999,\"oBC_ConnectSts_EV\":1,\"discountPower\":31,\"aoiId\":\"-999999\",\"chrgLogicNum\":1,\"oBC_ChrgrOutputCurr\":8.0,\"end_BMS_RMC_ModuleTempMin\":7}\n";
    public static JSONObject data = JSON.parseObject(txtJson);
    private Integer count = 1;
    private Integer countMax = 1000;
    private boolean isRunning = true;
    private ArrayList<String> fileList = new ArrayList<>();

    public static void main(String[] args) {
        JSONObject data = JSON.parseObject(txtJson);
        System.out.println(data);
        System.out.println(data.getString("BMS_SWVersion"));
        data.put("BMS_SWVersion", data.getInteger("BMS_SWVersion") + 1);

    }


    @Override
    public void run(SourceContext<String> sourceContext) throws Exception {
        while (isRunning) {
            data.put("BMS_SWVersion", count);
            sourceContext.collect(JSONObject.toJSONString(data));
            count++;
            if (count >= countMax) {
                return;
            }
            System.out.println("index: " + count);
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        isRunning = false;
    }
}
